package com.foreverwin.jzymes.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.app.service.IResourceStatusChangeAdviceService;
import com.foreverwin.jzymes.common.service.IResourceAdviceService;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.annotation.ParamBind;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/app/resource")
public class ResourceController {

    @Autowired
    IResourceAdviceService resourceAdviceService;
    @Autowired
    IResourceStatusChangeAdviceService resourceStatusChangeAdviceService;
    @Autowired
    JmsMessagingTemplate jmsTemplate;

    @RequestMapping("/findResourceStatus")
    @ParamBind
    public AjaxResult findResourceStatus( String resource ){
        try{
            String site = CommonMethods.getSite();
            Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));
            Map<String,Object> resourceMap = resourceAdviceService.existResource( site, resource );
            String currentStatus = resourceAdviceService.findResourceStatus( site, resource );
            Map<String,Object> valueMap = new HashMap<>();
            valueMap.put( "resource", resource );
            valueMap.put( "resourceDesc", resourceMap != null ? resourceMap.get("DESCRIPTION") : "" );
            valueMap.put( "status", currentStatus );
            valueMap.put( "nextStatus", resourceAdviceService.findNextStatus( site, resource, currentStatus ) );
            return AjaxResult.success().put( "value", valueMap );

        }catch ( Exception e ){
            return AjaxResult.error( e.getMessage() );
        }
    }

    @RequestMapping("/changeResourceStatus")
    @ParamBind
    public AjaxResult changeResourceStatus( String resource, String status, String breakDesc ){

        try{
            String site = CommonMethods.getSite();
            resourceStatusChangeAdviceService.changeResourceStatus( site, resource, status, breakDesc );

            /**发送设备状态变化到页面*/
            JSONObject eventData = new JSONObject();
            eventData.put( "SITE", site );
            eventData.put( "RESRCE", resource );
            eventData.put( "STATUS", status );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "RESOURCE_STATUS_CHANGE" );
            appEvent.setData( eventData );
            ActiveMQTopic topic = new ActiveMQTopic( "MES_APP_EVENT_INFRA" );
            jmsTemplate.convertAndSend( topic, appEvent );

            return AjaxResult.success();
        }catch ( Exception e ){
            return AjaxResult.error( e.getMessage() );
        }

    }

}
